home *** CD-ROM | disk | FTP | other *** search
- Path: news.nstn.ca!news
- From: mgemmell@transres.com
- Newsgroups: comp.lang.c++
- Subject: Re: casting a void pointer back to a function pointer
- Date: Thu, 04 Apr 1996 20:48:18 GMT
- Organization: Nova Scotia Technology Network
- Message-ID: <4k1cut$75u@news.nstn.ca>
- References: <DLABELL.96Apr4021045@columbia.pcs.cnu.edu>
- NNTP-Posting-Host: pc-007.transres.com
- X-Newsreader: Forte Free Agent 1.0.82
-
- dlabell@pcs.cnu.edu (Daniel LaBell) wrote:
-
- >I'm going to skip explaining why I want to do this, and get right to the
- >problem. How do I cast a pointer to a function pointer?
-
- >Here is trivial example that shows the problem.
-
- >void foo1(){ cout << " foo1 " << endl; }
- >void foo2(){ cout << " foo2 " << endl; }
- >void foo3( int x )
- >{ cout << " foo3, argument = " << x << endl; }
-
- >void do_a_foo( void (*fun)() )
- >{ (*fun)(); }
- >void do_a_foo2( int x, void (*fun) (int ) )
- >{ (*fun) ( x ); }
- >int main()
- >{
- > do_a_foo ( foo1 );
- > do_a_foo ( foo2 );
- > do_a_foo2 ( 2, foo3 );
- > void * x=foo2;
- > do_a_foo (x); // I don't know the syntax to do this cast. :(
- do_a_foo((void (*)())x);
- > return 0;
- >}
- >I get this warning message from g++:
- >:/home/student/dlabell/C/test.cc: In function `int main()':
- >:/home/student/dlabell/C/test.cc:21: warning: ANSI C++ forbids implicit conversion from `void *' in argument passing
- >:
- >:Compilation finished at Thu Apr 4 01:18:20
-
- <snip>
- >Daniel LaBell
-
- Or if you want to do a typedef try the following.
- typedef void (*vFnNoArgs)();
-
- then in your code you can have
- do_a_foo((vFnNoArgs)x);
-
- As far as I know this is all ANSI stuff.
-
- Hope this helps
- Mike Gemmell
- mgemmell@transres.com
-
-